//index.js
const app = getApp()
const {
  envList
} = require('../../envList.js')

Page({
  data: {
    showUploadTip: false,
    envList,
    selectedEnv: envList[0],
    haveCreateCollection: false
  },
  onShow(){
    var that = this;
    if (wx.getStorageSync('identity_id') == null || wx.getStorageSync('identity_id') == '') {
      that.setData({
        identity_id: 0,
        powerList: []
      });
    } else {
      // console.log('【首页】页面数据调用缓存。');
      that.setData({
        identity_id: wx.getStorageSync('identity_id'),
        powerList: wx.getStorageSync('powerList')
      });
    }
    that.setData({
      identity_id: wx.getStorageSync('identity_id'),
      powerList: wx.getStorageSync('powerList')
    });
  },
  onLoad(options) {
    var that = this;
    // 微信扫一扫小程序码入口
    if (options.scene != null) {
      that.setData({
        identity_id: 1
      });
      wx.setStorageSync('identity_id', 1);
      var scene = decodeURIComponent(options.scene);
      var card_number = scene.split('&')[0].split('=')[1];
      var card_password = scene.split('&')[1].split('=')[1];
      // console.log('scene:' + scene + ';card_number:' + card_number + ';card_password:' + card_password);
      that.toPicking(card_number + ',' + card_password);
    }
  },
  // 获取用户信息
  getUserinfo(initial_id) {
    wx.showLoading({
      title: '系统正在处理',
    })
    wx.cloud.callFunction({
      name: "getUserInfo",
      data: {
        identity_id: this.data.identity_id,
        initial_id: initial_id
      },
      success: res => {
        wx.hideLoading();
        this.setData({
          powerList: res.result.list
        })
        // 缓存
        wx.setStorageSync('openid', res.result.openid);
        wx.setStorageSync('powerList', res.result.list);
      }
    })
  },
  // 扫描二维码
  scanCode: function () {
    var that = this;
    wx.scanCode({
      onlyFromCamera: false,
      scanType: ['qrCode', 'barCode'],
      success: function (res) {
        // 扫码之后进行跳转
        that.toPicking(res.result);
      },
      fail: function (err) {
        // console.log(err)
        wx.showToast({
          title: '扫码失败',
          icon: 'error',
          duration: 2000
        })
      },
      complete: function (res) {
        // console.log(res)
      }
    })
  },
  // 跳转到【我的】
  toOrder() {
    wx.switchTab({
      url: '../order/index',
    })
  },
  // 右上角分享
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      // console.log(res.target)
    }
    return {
      title: '我要提货——在线提货，轻松便捷',
      path: '/pages/index/index',
      imageUrl: 'https://i0.hdslb.com/bfs/album/e6faffaa50b28f00c94e380154f4518745fd6d4d.jpg'
    }
  },
  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: '我要提货——在线提货，轻松便捷',
    }
  },
  // 前往提货信息页面
  toPicking(e) {
    var that = this;
    wx.showLoading({
      title: '系统正在处理',
    })
    let str = e.split(',');
    wx.cloud.callFunction({
      name: "judgeAccount",
      data: {
        card_number: str[0],
        card_password: str[1],
      },
      success: res => {
        wx.hideLoading();
        let list = res.result.list[0];
        // console.log(res);
        if (res.result.list.length != 0) {
          wx.navigateTo({
            url: '/pages/customer/picking/picking?card_id=' + list._id + '&card_number=' + str[0] + '&card_password=' + str[1] + '&goods_url=' + list.goods_url + '&goods_name=' + list.goods_name + '&goods_id=' + list.goods_id + '&goods_number=' + list.goods_number + '&goods_explain=' + list.goods_explain + '&specs=' + list.specs + '&type=' + list.type,
          });
        } else {
          wx.showToast({
            title: '提货码无效',
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  },
  // 卡密提货
  toCardPicking() {
    var that = this;
    that.setData({
      modalName: 'DialogModal'
    })
  },
  // 跳转页面
  navigateToPage(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.info,
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  ChooseCheckbox(e) {
    let items = this.data.checkbox;
    let values = e.currentTarget.dataset.value;
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      if (items[i].value == values) {
        items[i].checked = !items[i].checked;
        break
      }
    }
    this.setData({
      checkbox: items
    })
  },
  // 获取卡号
  getCardNumber(e) {
    var that = this;
    that.setData({
      card_number: e.detail.value
    })
  },
  // 获取卡密码
  getPassword(e) {
    var that = this;
    that.setData({
      card_password: e.detail.value
    })
  },
  // 卡密提货确定按钮
  cardSubmit(e) {
    if (this.data.card_number == null || this.data.card_number == '' || this.data.card_password == null || this.data.card_password == '') {
      wx.showToast({
        title: '卡密不能为空',
        icon: 'error'
      })
    } else {
      let str = this.data.card_number + ',' + this.data.card_password;
      this.toPicking(str);
    }
  },
  setCustomer() {
    this.setData({
      first_choose_id: 1
    })
  },
  setBusiness() {
    this.setData({
      first_choose_id: 2
    })
  },
  // 首次身份选择
  setIdentityId() {
    if(this.data.first_choose_id != null){
      this.setData({
        identity_id: this.data.first_choose_id
      });
      this.getUserinfo(1);
      // 缓存
      wx.setStorageSync('identity_id', this.data.first_choose_id);
    }else{
      wx.showToast({
        title: '请选择身份',
        icon: 'error'
      })
    }
  },
  // 切换身份
  changeIdentity(e) {
    let identity_id = e.currentTarget.dataset.info;
    this.setData({
      identity_id: identity_id,
    })
    this.getUserinfo(2);
    // 缓存
    wx.setStorageSync('identity_id', identity_id);
  }
})